package com.sia.springcloud.controller;


import com.alibaba.fastjson.JSON;
import com.sia.springcloud.model.Point;
import com.sia.springcloud.service.IPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import com.sia.springcloud.protocol.model.OpcuaPoint;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 通信协议变量表 前端控制器
 * </p>
 *
 * @author Sirius
 * @since 2021-12-17
 */
@Slf4j
@RestController
public class PointController implements IPointController {

    @Resource
    private IPointService pointService;


    /**
     * 添加点位
     *
     * @param request
     * @return
     */
    public String insertPoint(HttpServletRequest request) {
        Point point = packingPoint(request);
        return pointService.insertPoint(point);
    }


    /**
     * 分页查询点位
     *
     * @param request
     * @return
     */
    public String queryPoint(HttpServletRequest request) {
        String deviceId = request.getParameter("deviceId");
        int start = Integer.parseInt(request.getParameter("start"));
        int limit = Integer.parseInt(request.getParameter("limit"));

        return pointService.queryPoint(start, limit, deviceId);
    }

    /**
     * 删除点位
     *
     * @param request
     * @return
     */
    public String deletePoint(HttpServletRequest request) {
        String[] pointIdList = request.getParameter("pointId").split(",");

        return pointService.deletePoint(pointIdList);
    }


    /**
     * 修改点位
     *
     * @param request
     * @return
     */
    public String updatePoint(HttpServletRequest request) {

        Point point = packingPoint(request);

        return pointService.updatePoint(point);
    }


    /**
     * 获取点位表格格式
     *
     * @return
     */
    @Override
    public String tableStructure() {
        return null;
    }


    /**
     * 将请求参数包装到一个Point对象中并返回
     *
     * @param request
     * @return
     */
    private Point packingPoint(HttpServletRequest request) {
        String id = request.getParameter("id");
        String collect = request.getParameter("collect");
        String deviceId = request.getParameter("deviceId");
        String protocolName = request.getParameter("protocolName");
        String pointName = request.getParameter("pointName");
        String frequency = request.getParameter("frequency");
        String sendFrequency = request.getParameter("sendFrequency");
        String metricName = request.getParameter("metricName");
        String target = request.getParameter("target");
        String domain = request.getParameter("domain");
        String scale = request.getParameter("scale");
        String functionCode = request.getParameter("functionCode");
        String slaveId = request.getParameter("slaveId");
        String bit = request.getParameter("bit");
        String offset = request.getParameter("offset");
        String length = request.getParameter("length");
        String pattern = request.getParameter("pattern");
        String type = request.getParameter("type");

        Point point = new Point();
        if (null != id && !"".equals(id)) {
            point.setId(Integer.parseInt(id));
        }
        if (null != collect && !"".equals(collect)) {
            point.setCollect(Integer.parseInt(collect));
        }
        if (null != deviceId && !"".equals(deviceId)) {
            point.setDeviceId(Integer.parseInt(deviceId));
        }
        if (null != protocolName && !"".equals(protocolName)) {
            point.setProtocolName(protocolName);
        }
        if (null != pointName && !"".equals(pointName)) {
            point.setPointName(pointName);
        }
        if (null != frequency && !"".equals(frequency)) {
            point.setFrequency(Integer.parseInt(frequency));
        }
        if (null != sendFrequency && !"".equals(sendFrequency)) {
            point.setSendFrequency(Integer.parseInt(sendFrequency));
        }
        if (null != metricName && !"".equals(metricName)) {
            point.setMetricName(metricName);
        }
        if (null != target && !"".equals(target)) {
            point.setTarget(target);
        }
        if (null != domain && !"".equals(domain)) {
            point.setDomain(domain);
        }
        if (null != scale && !"".equals(scale)) {
            point.setScale(scale);
        }
        if (null != functionCode && !"".equals(functionCode)) {
            point.setFunctionCode(Integer.parseInt(functionCode));
        }
        if (null != slaveId && !"".equals(slaveId)) {
            point.setSlaveId(Integer.parseInt(slaveId));
        }
        if (null != bit && !"".equals(bit)) {
            point.setBit(Integer.parseInt(bit));
        }
        if (null != offset && !"".equals(offset)) {
            point.setOffset(Integer.parseInt(offset));
        }
        if (null != length && !"".equals(length)) {
            point.setLength(Integer.parseInt(length));
        }
        if (null != pattern && !"".equals(pattern)) {
            point.setPattern(Integer.parseInt(pattern));
        }
        if (null != type && !"".equals(type)) {
            point.setType(Integer.parseInt(type));
        }

        return point;
    }
}
